//
//  GJVaryitemPickView.m
//  YDGJ
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#import "GJVaryitemPickView.h"

#define GJColumnSpacing     (10)
#define GJRowSpacing        (10)
#define GJItemBtnHeight     (37)
#define GJRowMaxCount       (4)
#define GJDefaultBtnBaseTag (235)

@interface GJVaryitemPickView ()
@property(nonatomic,copy)void(^doneBlock)(NSArray<NSString *> *);
@property(nonatomic,strong)UIButton *chosenBtn;
@property(nonatomic,strong)NSArray *btnTitleArr;
@property(nonatomic,assign)BOOL isRadio;
@property(nonatomic,strong)NSMutableArray *pickTitleBtnArr;
@property(nonatomic,assign)int maxRows;

@end

@implementation GJVaryitemPickView

+(instancetype)gjVaryitemPickViewWithOrigin:(CGPoint)origin itemTitle:(NSString *)title conditionTitles:(NSArray<NSString *> *)titleArr methodPick:(GJMethodPick)pickMethod rowMaxCount:(int)maxRow handDoneBlock:(void(^)(NSArray<NSString *> *))block
{
    return [[self alloc]initWithOrigin:origin itemTitle:title conditionTitles:titleArr methodPick:pickMethod rowMaxCount:maxRow handDoneBlock:block];
}

-(instancetype)initWithOrigin:(CGPoint)origin itemTitle:(NSString *)title conditionTitles:(NSArray<NSString *> *)titleArr methodPick:(GJMethodPick)pickMethod rowMaxCount:(int)maxRow handDoneBlock:(void(^)(NSArray<NSString *> *))block
{
    if (self = [super initWithFrame:CGRectMake(origin.x, origin.y, Screen_Width, 0)])
    {
        self.doneBlock = block;
        self.btnTitleArr = titleArr;
        self.isRadio = GJRadioNone== pickMethod;
        self.maxRows = maxRow?:GJRowMaxCount;
        [self loadViewWithItemTitle:title conditionTitles:titleArr];
    }
    return self;
    
}
-(void)loadViewWithItemTitle:(NSString *)title conditionTitles:(NSArray<NSString *> *)titleArr
{
    if (![titleArr.lastObject isKindOfClass:[NSString class]] || !titleArr.count) return;
    self.backgroundColor = WhiteColor;
    UILabel *itemLabel = [[UILabel alloc]initWithFrame:CGRectMake(KViewLineSpacing, 16, Screen_Width-2*KViewLineSpacing, 18)];
    itemLabel.textColor = Color_BlackFont;
    itemLabel.font = FontCustomSize(16.f);
    itemLabel.text = title?:@"时间";
    [self addSubview:itemLabel];
    
    CGFloat width;UIButton *itemBtn;
    width = (self.width -(self.maxRows -1)*GJRowSpacing - 2*KViewLineSpacing)/self.maxRows;
    
    for (int i =0; i < titleArr.count; i++)
    {
        itemBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        itemBtn.titleLabel.font = FontCustomSize(14.f);
        itemBtn.frame = CGRectMake(KViewLineSpacing+i%self.maxRows*(width+GJRowSpacing), CGRectGetMaxY(itemLabel.frame)+16+i/self.maxRows*(GJItemBtnHeight+GJColumnSpacing), width, GJItemBtnHeight);
        
        [itemBtn setTitle:titleArr[i] forState:UIControlStateNormal];
        [itemBtn setTitleColor:Color_BlackFont forState:UIControlStateNormal];
        [itemBtn setTitleColor:WhiteColor forState:UIControlStateSelected];
        [itemBtn setBackgroundImage:imageWithColor(Color_Main) forState:UIControlStateSelected];
        [itemBtn setBackgroundImage:imageWithColor(GJ_Btn_DisabledColor) forState:UIControlStateDisabled];
        itemBtn.tag = GJDefaultBtnBaseTag+i;
        itemBtn.layer.cornerRadius = 3;
        itemBtn.layer.masksToBounds = YES;
        itemBtn.layer.borderColor = RGB(232, 232, 232).CGColor;
        itemBtn.layer.borderWidth = 1.f;
        
        [itemBtn addTarget:self action:@selector(itemBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:itemBtn];
    }
    self.height = CGRectGetMaxY(itemBtn.frame)+16;
}
-(NSMutableArray *)pickTitleBtnArr
{
    if (nil == _pickTitleBtnArr) {
        _pickTitleBtnArr = [NSMutableArray new];
    }
    return _pickTitleBtnArr;
}

-(void)itemBtnClicked:(UIButton *)sender
{
    sender.selected = !sender.selected;
    [self itemBtnBorderStatus:sender];
    NSArray *tempArr;
    if (self.isRadio) {
        self.chosenBtn.selected = !self.chosenBtn.selected;
        [self itemBtnBorderStatus:self.chosenBtn];
        self.chosenBtn = sender;
        tempArr = @[sender.titleLabel.text];
    }else{
        if (sender.selected) {
            if (![self.pickTitleBtnArr containsObject:sender.titleLabel.text])[self.pickTitleBtnArr addObject:sender.titleLabel.text];
        }else{
            [self.pickTitleBtnArr removeObject:sender.titleLabel.text];
        }
        tempArr = self.pickTitleBtnArr;
    }
    if (self.doneBlock)self.doneBlock(tempArr);
    tempArr = nil;
}
-(void)itemBtnBorderStatus:(UIButton *)sender
{
    if (!sender) return;
    sender.layer.borderColor = !sender.selected?RGB(232, 232, 232).CGColor:[UIColor clearColor].CGColor;
    sender.layer.borderWidth = !sender.selected?1.f:0.f;
}

-(void)setSelectBtnWithTitle:(NSArray<NSString *> *)titleArr;
{
    if (!titleArr.count) return;
    if (self.isRadio) {
        if (![self.btnTitleArr containsObject:titleArr.firstObject])return;
        UIButton *itemBtn = [self viewWithTag:[self.btnTitleArr indexOfObject:titleArr.firstObject]+GJDefaultBtnBaseTag];
        [self itemBtnClicked:itemBtn];
    }else{
        for (int i = 0; i<titleArr.count; i++) {
            if (![self.btnTitleArr containsObject:titleArr[i]])continue;
            UIButton *itemBtn = [self viewWithTag:[self.btnTitleArr indexOfObject:titleArr[i]]+GJDefaultBtnBaseTag];
            [self itemBtnClicked:itemBtn];
        }
    }
   
}

-(void)resetStatus
{
    if (self.isRadio) {
        self.chosenBtn.selected = !self.chosenBtn.selected;
        [self itemBtnBorderStatus:self.chosenBtn];
        self.chosenBtn = nil;
    }else{
        for (int i = 0; i < self.btnTitleArr.count; i++) {
            UIButton *itemBtn = [self viewWithTag:i+GJDefaultBtnBaseTag];
            itemBtn.selected = NO;
            [self itemBtnBorderStatus:itemBtn];
        }
    }
    if (self.doneBlock)self.doneBlock(nil);

}

@end
